package testebancodedados;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.*;

public class LocacaoForm {
    private static LocacaoControl control = new LocacaoControl();
    private static JPanel pCodCliente, pCodFilme, pSaida;
    private static JTextField codCliente, codFilme, saida;
    private static JButton button;
    private static JComboBox op;
    
    public LocacaoForm() {
    }
    
    public JPanel novoForm(){
        JPanel form = new JPanel();
        form.setLayout(new FlowLayout());
       
        JPanel panel = new JPanel(new GridLayout(6,1));
        form.add(panel);
      
        String[] array = new String[4];
        array[0] = "Locar Filme";
        array[1] = "Devolver Filme";
        array[2] = "Verificar Cliente";
        array[3] = "Verificar Filme";
        JComboBox comboBox = new JComboBox(array);
        op = comboBox;
        panel.add(comboBox);
        ChoiceListener itemListener = new ChoiceListener() {};
        comboBox.addItemListener(itemListener);
      
        JPanel row = new JPanel();
        pCodCliente = row;
        JLabel label = new JLabel("Código Cliente: "); // construct Label
        row.add(label);
        JTextField text = new JTextField("0000", 10);
        codCliente = text;
        row.add(text);
        panel.add(row);
      
        row = new JPanel();
        pCodFilme = row;
        label = new JLabel("Código Filme: "); // construct Label
        row.add(label);
        text = new JTextField("0000", 10);
        codFilme = text;
        row.add(text);
        panel.add(row);

        button = new JButton("Locar Filme"); // construct Button
        panel.add(button);
        ButtonListener buttonListener = new ButtonListener();
        button.addActionListener(buttonListener);

        row = new JPanel();
        pSaida = row;
        text = new JTextField(10);
        text.setEditable(false);
        saida = text;
        row.add(text);
        panel.add(row);
        pSaida.setVisible(false);
        
        form.setSize(300, 300);
        form.setVisible(true);
        return form;
    }
    
    private static class ChoiceListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            button.setLabel(op.getSelectedItem().toString());
            switch (op.getSelectedItem().toString()) {
                case "Locar Filme":
                    pCodCliente.setVisible(true);
                    pCodFilme.setVisible(true);
                    pSaida.setVisible(false);
                    break;
                case "Devolver Filme":
                    pCodCliente.setVisible(true);
                    pCodFilme.setVisible(true);
                    pSaida.setVisible(false);
                    break;
                case "Verificar Cliente":
                    pCodCliente.setVisible(true);
                    pCodFilme.setVisible(false);
                    pSaida.setVisible(true);
                    break;
                case "Verificar Filme":
                    pCodCliente.setVisible(false);
                    pCodFilme.setVisible(true);
                    pSaida.setVisible(true);
                    break;
            }
        }
    }
    
    private static class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String codigoCliente = codCliente.getText();
            String codigoFilme = codFilme.getText();
            ArrayList<String> list;
            
            switch (op.getSelectedItem().toString()) {
                case "Locar Filme":
                    control.locar(codigoCliente, codigoFilme);
                    break;
                case "Devolver Filme":
                    control.devolver(codigoCliente, codigoFilme);
                    break;
                case "Verificar Cliente":
                    list = control.verCliente(codigoCliente);
                    saida.setText(list.get(0));
                    break;
                case "Verificar Filme":
                    list = control.verFilme(codigoFilme);
                    saida.setText(list.get(0));
                    break;
            }
        }
    }
   
}